using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Data.SQLite;
using System.Linq;
using System.Threading.Tasks;


namespace sn.Models
{

    public class DaoFolder {        

        static string create_table_sql = "create table folder_tbl("
            + " new_name text not null,ori_name text not null); ";

        static string drop_table_sql = "drop table folder_tbl;";

        public static void create_tbl(){
            SQLiteHelper.ExecuteNonQuery(create_table_sql,null);
        }


        public static void drop_tbl(){
            SQLiteHelper.ExecuteNonQuery(drop_table_sql,null);
        }

        public static bool table_exists(){
            string sql = "select count(*) from sqlite_master where type = 'table' and name = 'folder_tbl'";
            int ret = Convert.ToInt32(SQLiteHelper.ExecuteScalar(sql,null).ToString());
            if(ret==0){
                return false;
            }
            return true;
        }


        public static void insert(string new_name,string ori_name){
            string sql = "insert into folder_tbl(new_name,ori_name) values(@new_name,@ori_name)";
            SQLiteParameter[] paras = new SQLiteParameter[2];            
            paras[0] = new SQLiteParameter();
            paras[0].ParameterName = "@new_name";
            paras[0].Value = new_name;
            paras[1] = new SQLiteParameter();
            paras[1].ParameterName = "@ori_name";
            paras[1].Value = ori_name;
            SQLiteHelper.ExecuteNonQuery(sql,paras);
        }

        public static bool exists_ori(string ori_name){
            string sql = "select count(*) from folder_tbl where ori_name=@ori_name";
            SQLiteParameter[] paras = new SQLiteParameter[1];
            paras[0] = new SQLiteParameter();
            paras[0].ParameterName = "@ori_name";
            paras[0].Value = ori_name;          
            int ret = Convert.ToInt32(SQLiteHelper.ExecuteScalar(sql,paras).ToString());
            if(ret==0){
                return false;
            }
            return true;

        }

        public static bool exists_new(string new_name){
            string sql = "select count(*) from folder_tbl where new_name=@new_name";
            SQLiteParameter[] paras = new SQLiteParameter[1];          
            paras[0] = new SQLiteParameter();
            paras[0].ParameterName = "@new_name";
            paras[0].Value = new_name;
            int ret = Convert.ToInt32(SQLiteHelper.ExecuteScalar(sql,paras).ToString());
            if(ret==0){
                return false;
            }
            return true;

        }

        public static string get_old_name(string new_name){
             string sql = "select ori_name from folder_tbl where new_name=@new_name";
            SQLiteParameter[] paras = new SQLiteParameter[1];         
            paras[0] = new SQLiteParameter();
            paras[0].ParameterName = "@new_name";
            paras[0].Value = new_name;
            string ori_name = SQLiteHelper.ExecuteScalar(sql,paras).ToString();            
            return ori_name;

        }

        public static string get_new_name(string ori_name){
            string sql = "select new_name from folder_tbl where ori_name=@ori_name";
            SQLiteParameter[] paras = new SQLiteParameter[1];         
            paras[0] = new SQLiteParameter();
            paras[0].ParameterName = "@ori_name";
            paras[0].Value = ori_name;
            string new_name = SQLiteHelper.ExecuteScalar(sql,paras).ToString();            
            return new_name;

        }

    }

}